﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using YDModel;
using YDServices.Comm;

namespace YDServices
{
    public class CustomerService : BaseService
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="conds"></param>
        /// <returns></returns>
        public List<T_Customers> getList(ConditionItem[] conds)
        { 
             try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var list = db.T_Customers.Where(o=>o.State!=false).AsQueryable();
                    if (conds != null)
                    {
                        var qb = QueryBuilder<T_Customers>.Create().AddConditions(conds);
                        list = list.Where(qb.ToExpression());
                    }
                    return list.ToList();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        /// <summary>
        /// 下拉框
        /// </summary>
        /// <returns></returns>
        public List<T_Customer_List> getComboxList(string customerName)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                { 
                    List<T_Customer_List> list = db.T_Customers.Where(o => o.State != false)
                        .Select(o => new T_Customer_List { ID = o.ID, CustomerName = o.CustomerName }).ToList();
                    T_Customer_List tf = new T_Customer_List();

                    tf.ID = 0;
                    tf.CustomerName = customerName;
                    list.Add(tf);

                    return list;
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        public List<T_Customer_List> getComboxList()
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    List<T_Customer_List> list = db.T_Customers.Where(o => o.State != false)
                        .Select(o => new T_Customer_List { ID = o.ID, CustomerName = o.CustomerName }).ToList(); 

                    return list;
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

            /// <summary>
            /// 新增
            /// </summary>
            /// <param name="tu"></param>
            public void AddItem(T_Customers tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var user = db.T_Customers.Where(o => o.CustomerName == tu.CustomerName && o.State == true).FirstOrDefault();
                    if (user != null)
                    {
                        throw new ApplicationException("客户已存在!");
                    }

                    tu.State = true;
                    tu.CreatedTime = GetSqlNow();
                    tu.CreatedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    db.T_Customers.Add(tu);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tu"></param>
        public void EditItem(T_Customers tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var item = db.T_Customers.Where(o => o.ID == tu.ID && o.State == true).FirstOrDefault();
                    if (item == null)
                    {
                        throw new ApplicationException("客户信息不存在!");
                    }
 
                    item.CustomerName = tu.CustomerName;
                    item.Phone = tu.Phone;
                    item.Address = tu.Address;
                    item.Memo = tu.Memo;
                    item.ModifiedTime = GetSqlNow();
                    item.ModifiedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="tu"></param>
        /// <param name="loginUser"></param>
        public void DelItem(T_Customers item)  //List<T_Customers> tuList 
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    
                    var fact = db.T_Customers.Where(o => o.ID == item.ID ).FirstOrDefault();
                    if (fact == null)
                    {
                        throw new ApplicationException("客户信息不存在!");
                    }

                    var sendList = db.T_Sending.Where(o => o.Customer_ID == item.ID).ToList();
                    if(sendList.Count>0)
                    {
                        throw new ApplicationException("该客户信息已被使用,不允许删除!");
                    } 

                    fact.State = false;
                    fact.DeletedTime = GetSqlNow();
                    fact.DeletedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }

        

    }
}
